﻿Partial Public Class MainPage
    Inherits UserControl
    Private SelectedDocument As DocumentTab = Nothing

#Region "New"

    Public Sub New()
        InitializeComponent()
        Dim tab As New DocumentTab
        tab.Header = "New Document"
        TabCell.Items.Add(tab)
        TabCell.SelectedItem = tab
    End Sub

#End Region

#Region "Buttons"

#Region "NewButton"

    Private Sub NewButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles NewButton.Click
        Dim tab As New DocumentTab
        tab.Header = "New Document"
        TabCell.Items.Add(tab)
        TabCell.SelectedItem = tab
        tab.Editor.Focus()
    End Sub

#End Region

#Region "OpenButton"

    Private Sub OpenButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles OpenButton.Click
        Dim o As New OpenFileDialog
        o.Filter = "Supported Documents(.xaml)|*.xaml;|All Files(*.*)|*.*"
        If o.ShowDialog Then
            If o.File.Extension = ".xaml" Then
                NewButton_Click(Nothing, Nothing)
                SelectedDocument.Header = o.File.Name
                Dim r As IO.StreamReader = o.File.OpenText
                SelectedDocument.Editor.Xaml = r.ReadToEnd
                r.Close()
            Else
                'TODO:
                'NewButton_Click(Nothing, Nothing)
                'SelectedDocument.Header = o.File.Name
                'SelectedDocument.Editor.Selection.Select(SelectedDocument.Editor.ContentStart, SelectedDocument.Editor.ContentEnd)
                'SelectedDocument.Editor.Selection.Text = IO.File.ReadAllText(o.File.FullName)
            End If
        End If
    End Sub

#End Region

#Region "CloseButton"

    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        TabCell.Items.Remove(SelectedDocument)
    End Sub

#End Region

#Region "SaveButton"

    Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles SaveButton.Click
        Dim s As New SaveFileDialog
        s.Filter = "Supported Documents(.xaml)|*.xaml"
        If s.ShowDialog Then
            Using fs As IO.FileStream = DirectCast(s.OpenFile(), IO.FileStream)
                Dim enc As New System.Text.UTF8Encoding()
                Dim buffer As Byte() = enc.GetBytes(SelectedDocument.Editor.Xaml)
                fs.Write(buffer, 0, buffer.Length)
                fs.Close()
            End Using
        End If
    End Sub

#End Region

#Region "Edit"

#Region "Undo/Redo"

    Private Sub UndoButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        'TODO:
    End Sub

    Private Sub RedoButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        'TODO:
    End Sub

#End Region

#Region "Cut/Copy/Paste Buttons"

    Private Sub CutButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles CutButton.Click
        SelectedDocument.Editor.Selection.Text = ""
        SelectedDocument.Editor.Focus()
    End Sub

    Private Sub CopyButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles CopyButton.Click
        Clipboard.SetText(SelectedDocument.Editor.Selection.Text)
        SelectedDocument.Editor.Focus()
    End Sub

    Private Sub PasteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles PasteButton.Click
        If Clipboard.ContainsText Then
            SelectedDocument.Editor.Selection.Text = Clipboard.GetText
        End If
        SelectedDocument.Editor.Focus()
    End Sub

#End Region

#Region "Select All"

    Private Sub SelectAllButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SelectAllButton.Click
        SelectedDocument.Editor.Selection.Select(SelectedDocument.Editor.ContentStart, SelectedDocument.Editor.ContentEnd)
        SelectedDocument.Editor.Focus()
    End Sub

#End Region

#End Region

#Region "AboutButton"

    Private Sub AboutButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles AboutButton.Click
        Dim d As New AboutDialog
        d.Show()
    End Sub

#End Region

#End Region

#Region "TabCell"

    Private Sub TabCell_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles TabCell.SelectionChanged
        If TabCell.SelectedItem IsNot Nothing Then
            SelectedDocument = TabCell.SelectedItem
            UpdateButtons()
        Else
            SelectedDocument = Nothing
            UpdateButtons()
        End If
    End Sub

#End Region

#Region "Reuse Code"

    Private Sub UpdateButtons()
        If SelectedDocument IsNot Nothing Then
            CloseButton.IsEnabled = True
            SaveButton.IsEnabled = True
            PrintButton.IsEnabled = True
            CutButton.IsEnabled = True
            CopyButton.IsEnabled = True
            PasteButton.IsEnabled = True
            SelectAllButton.IsEnabled = True
            BoldButton.IsEnabled = True
            ItalicButton.IsEnabled = True
            UnderlineButton.IsEnabled = True
        Else
            CloseButton.IsEnabled = False
            SaveButton.IsEnabled = False
            PrintButton.IsEnabled = False
            CutButton.IsEnabled = False
            CopyButton.IsEnabled = False
            PasteButton.IsEnabled = False
            SelectAllButton.IsEnabled = False
            BoldButton.IsEnabled = False
            ItalicButton.IsEnabled = False
            UnderlineButton.IsEnabled = False
        End If
    End Sub

#End Region

    Private Sub BoldButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles BoldButton.Click
        If SelectedDocument.Editor.Selection.Text.Length > 0 Then
            If TypeOf SelectedDocument.Editor.Selection.GetPropertyValue(Run.FontWeightProperty) Is FontWeight AndAlso DirectCast(SelectedDocument.Editor.Selection.GetPropertyValue(Run.FontWeightProperty), FontWeight) = FontWeights.Normal Then
                SelectedDocument.Editor.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold)
            Else
                SelectedDocument.Editor.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal)
            End If
        End If
        SelectedDocument.Editor.Focus()
    End Sub

    Private Sub ItalicButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ItalicButton.Click
        If SelectedDocument.Editor.Selection.Text.Length > 0 Then
            If TypeOf SelectedDocument.Editor.Selection.GetPropertyValue(Run.FontStyleProperty) Is FontStyle AndAlso DirectCast(SelectedDocument.Editor.Selection.GetPropertyValue(Run.FontStyleProperty), FontStyle) = FontStyles.Normal Then
                SelectedDocument.Editor.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic)
            Else
                SelectedDocument.Editor.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal)
            End If
        End If
        SelectedDocument.Editor.Focus()
    End Sub

    Private Sub UnderlineButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles UnderlineButton.Click
        If SelectedDocument.Editor.Selection.Text.Length > 0 Then
            If SelectedDocument.Editor.Selection.GetPropertyValue(Run.TextDecorationsProperty) Is Nothing Then
                SelectedDocument.Editor.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline)
            Else
                SelectedDocument.Editor.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, Nothing)
            End If
        End If
        SelectedDocument.Editor.Focus()
    End Sub
End Class